package jflowmap.util;

import java.awt.Color;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

public class Colors {
    private static Map<String, Map<String, Color>> colorMap = new HashMap<String, Map<String, Color>>();
    private static Map<String, Map<String, Color>> lightColorMap = new HashMap<String, Map<String, Color>>();
    private static Map<String, Map<String, Color>> transparentColorMap = new HashMap<String, Map<String, Color>>();
    private static Map<String, Map<String, String>> colorLabels = new HashMap<String, Map<String, String>>();
    
    private static List<Color> colorSet;
    private static int currentColor = 0;
    
    private static Map<String, Color> predefinedColors = new HashMap<String, Color>();
    
    public static void setPredefinedColor (String colorName, int color) {
    	predefinedColors.put(colorName, new Color(color));
    }
    
    private static Color predefinedColorFor (String colorName) {
    	if (predefinedColors.containsKey(colorName)) {
    		return predefinedColors.get(colorName);
    	}
    	
    	return null;
    }

    static {
    	colorSet = new ArrayList<Color>();
    	colorSet.add(new Color(250, 20, 20, 240));
    	colorSet.add(new Color(20, 250, 20, 240));
    	colorSet.add(new Color(20, 20, 250, 240));
    	colorSet.add(new Color(250, 250, 20, 240));
    	colorSet.add(new Color(250, 20, 250, 240));
    	colorSet.add(new Color(20, 250, 250, 240));

    	colorSet.add(new Color(200, 200, 200, 240));
    	
    	colorSet.add(new Color(250, 150, 0, 240));
    	colorSet.add(new Color(250, 0, 150, 240));
    	colorSet.add(new Color(0, 150, 250, 240));
    	colorSet.add(new Color(150, 250, 0, 240));
    	colorSet.add(new Color(150, 0, 250, 240));
    	colorSet.add(new Color(0, 250, 150, 240));
    	
    	colorSet.add(new Color(250, 150, 0, 240));
    	colorSet.add(new Color(250, 0, 150, 240));
    	colorSet.add(new Color(0, 150, 250, 240));
    	colorSet.add(new Color(150, 250, 0, 240));
    	colorSet.add(new Color(150, 0, 250, 240));
    	colorSet.add(new Color(0, 250, 150, 240));
    	
    	colorSet.add(new Color(100, 100, 100, 240));
    	
    }
    
    public static String getColorLabel (String colorName, String hint) {
    	if (Colors.colorLabels.containsKey(hint)) {
    		Map<String, String> labels = Colors.colorLabels.get(hint);
    		
    		if (labels.containsKey(colorName)) 
    			return labels.get(colorName);
    	}
    	
    	return null;
    }
    
    public static Iterable<String> getColorNames (String hint) {
    	List<String> colorLabels = new Vector<String>(); 
    	
    	for (String key: colorMap.keySet()) {
    		if (colorMap.get(key).containsKey(hint)) {
    			colorLabels.add(key);
    		}
    	}
    	
    	Collections.sort(colorLabels);
    	
    	return colorLabels;
    }
    

	public static void setLabelFor (String hint, String name, String label) {
		if (!colorLabels.containsKey(hint)) {
			colorLabels.put(hint, new HashMap<String,String>());
		}
		colorLabels.get(hint).put(name, label);
	}
    
    public static Color getColorFor (String name, String hint) {
    	
    	Color numberedColor = predefinedColorFor(name);
    	
    	if (!colorMap.containsKey(name)) {
    		colorMap.put(name, new HashMap<String, Color>());
        } 
    	
    	final Map<String, Color> hintMap = colorMap.get(name);
    	if (!hintMap.containsKey(hint)) {
    		hintMap.put(hint, numberedColor == null ? colorSet.get(currentColor++) : numberedColor);
    		if (currentColor >= colorSet.size()) 
        		currentColor = 0;
    	}
        
        return hintMap.get(hint);
    }
    
    public static Color getTransparentColorFor (String label, String hint) {
    	if (!transparentColorMap.containsKey(label)) {
    		transparentColorMap.put(label, new HashMap<String, Color>());
    	}
    	
    	final Map<String, Color> hintMap = transparentColorMap.get(label);
    	if (!hintMap.containsKey(hintMap)) {
    		final Color c = getColorFor(label, hint);
    		Color n = new Color(c.getRed(), c.getGreen(), c.getBlue(), 100);
    		hintMap.put(hint, n);
    	}
    	
    	return hintMap.get(hint);
    }
    
    public static Color getLightColorFor (String label, String hint) {
    	if (!lightColorMap.containsKey(label)) {    
    		lightColorMap.put(label, new HashMap<String, Color>());
    	}
    	
    	final Map<String, Color> hintMap = lightColorMap.get(label);
    	if (!hintMap.containsKey(hint)) {
    		final Color c = getColorFor(label, hint);

    		int r = c.getRed()+100;
    		if (r > 255) r = 255;
    		int g = c.getGreen()+100;
    		if (g > 255) g = 255;
    		int b = c.getBlue()+100;
    		if (b > 255) b = 255;

    		Color n = new Color(r, g, b);
    		hintMap.put(hint, n);
    	}
    	
		return hintMap.get(hint);
    }
}
